package vip.finance.makemoney.config.schedule;

import cn.hutool.http.HttpRequest;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import vip.finance.makemoney.common.Constant;
import vip.finance.makemoney.service.ScheduleService;

import java.time.DayOfWeek;
import java.time.LocalDateTime;

/**
 * 定时器
 *
 * @author yangbin
 * @since 2023-02-09
 * cron表达式：指定任务在特定时间执行
 * fixedDelay:表示上一次任务执行完成后多久再执行，参数类型long,单位：ms
 * fixedDelayString:与fixedDelay一样，只是参数类型是String
 * fixedRate:表示按一定的频率执行任务，参数类型long,单位：ms 如： fixedRate(5000)，表示这个定时器任务每5秒执行一次
 * fixedRateString:与fixedRate一样，只是参数类型变为String
 * initialDelay:表示延迟多久再第一次执行任务，参数类型为long ,单位：ms
 * initialDelayString：与initialDelay一样，只是参数类型String
 */
@Slf4j
@Component
public class ScheduleTask {

    private Integer time = 0;
    @Resource
    private ScheduleService scheduleService;

    //每小时刷新一次雪球token
    @Scheduled(cron = "0 0 * * * ? ")
    private void xqAToken() {
        Constant.XQ_A_TOKEN = String.valueOf(HttpRequest.get("https://xueqiu.com").execute().getCookie("xq_a_token"));
    }

    //定时刷新-A股
    @Scheduled(cron = "0/2 * 9-15 * * ?")
    private void china() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        if (dayOfWeek == DayOfWeek.SUNDAY || dayOfWeek == DayOfWeek.SATURDAY) {
            return;
        }
        if (now.getHour() < 9 || now.getHour() > 15) {
            return;
        }
        log.info("大A股自选刷新");
        scheduleService.refreshUserStockBySnow("CN");
    }

    //定时刷新-港股
    @Scheduled(cron = "0/2 * 9-16 * * ?")
    private void hongKong() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        if (dayOfWeek == DayOfWeek.SUNDAY || dayOfWeek == DayOfWeek.SATURDAY) {
            return;
        }
        log.info("港股自选刷新");
        scheduleService.refreshUserStockBySnow("HK");
        scheduleService.refreshZhiShu();
    }

    //定时刷新-美股
    @Scheduled(cron = "0/2 * * * * ?")
    private void american() {
        LocalDateTime now = LocalDateTime.now();
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        if (now.getHour() > 9 && now.getHour() < 16 || dayOfWeek == DayOfWeek.SUNDAY && now.getHour() < 16 || dayOfWeek == DayOfWeek.SATURDAY && now.getHour() > 9) {
            return;
        }
        log.info("美股自选刷新");
        scheduleService.refreshUserStockBySnow("US");
        scheduleService.refreshZhiShu();
    }

    //定时刷新-A股 每小时全量刷新一次
    @Scheduled(cron = "0 0 9-16 * * ? ")
    private void refreshAllCN() {
        scheduleService.refreshAllStockBySnow("CN");
    }

    //定时刷新-港股 全量刷新一次
    @Scheduled(cron = "0 10 9-16 * * ? ")
    private void refreshAllHK() {
        scheduleService.refreshAllStockBySnow("HK");
    }

    //定时刷新-美股 全量刷新一次
    @Scheduled(cron = "0 0 3-8 * * ? ")
    private void refreshAllUS() {
        scheduleService.refreshAllStockBySnow("US");
    }

    @Scheduled(cron = "40 4 20 * * ? ")
    private void refreshAllStockBySnow() {
        scheduleService.refreshAllStockBySnow("CN");
    }
}
